package com.fangcun.service.api.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.api.agent.order.request.AgentOrderRequest;
import com.fangcun.bean.api.agent.order.request.ConfirmProiceRequest;
import com.fangcun.bean.api.agent.order.response.AgentOrderForResponse;
import com.fangcun.bean.api.agent.order.response.AgentOrderResponse;
import com.fangcun.bean.api.order.request.OrderDetailRequest;
import com.fangcun.bean.api.order.response.OrderDetailResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.canstant.PublicConstant;
import com.fangcun.common.enums.OrderStatusEnum;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.AgentOrder;
import com.fangcun.mapper.AgentOrderMapper;
import com.fangcun.mapper.OrderMapper;
import com.fangcun.service.api.AgentOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AgentOrderServiceImpl implements AgentOrderService {
	@Autowired
	private AgentOrderMapper agentOrderMapper;
	@Autowired
	private OrderMapper orderMapper;
	// @Autowired
	// private AsyncMethod asyncMethod;

	@Override
	public PageInfo<AgentOrderResponse> orderList(PageBean<AgentOrderRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<AgentOrderResponse> list = agentOrderMapper.selectByOrder(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<AgentOrderForResponse> list(PageBean<AgentOrder> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<AgentOrderForResponse> list = agentOrderMapper.listForApiByAgentId(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void confirmProice(ConfirmProiceRequest request) throws ProAffairException {
		// 根据支付订单号查询订单
		OrderDetailRequest requestParam = new OrderDetailRequest();
		requestParam.setOrderNumber(request.getOrderNumber());
		OrderDetailResponse order = orderMapper.detailForApi(requestParam);
		if (order == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		AgentOrder agentorder = agentOrderMapper.selectByOrderId(order.getId());
		if (agentorder == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		if (agentorder.getAgentId() != request.getAgentId()) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "无权操作该订单");
		}
		// 是否是线下支付
		if (order.getIsOnlinePayment() != PublicConstant.OFF_LINE_PAYMENT) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "已经线上支付过了");
		}
		if (OrderStatusEnum.MAIN_ORDER_STATUS_1.getStatus().intValue() != order.getOrderStatus().intValue()) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "该订单已经处理过了：" + order.getOrderStatus());
		}
		order.setPayType(0);
		order.setOrderStatus(OrderStatusEnum.MAIN_ORDER_STATUS_2.getStatus());
		order.setPayTime(new Date());
		if (orderMapper.updateByPrimaryKeySelective(order) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "修改订单失败,需人工排查订单");
		}

	}
}
